
cmake_minimum_required( VERSION 2.6 )

macro( ADD_TEST test_name )
    add_subdirectory( ${test_name} )

    set( TEST_SRC_ )
    foreach( src_file ${TEST_SRC} )
        set( TEST_SRC_ ${TEST_SRC_} ${test_name}/${src_file} )
    endforeach( src_file ${TEST_SRC} )

    if( WIN32 )
        add_executable( ${test_name} WIN32 ${TEST_SRC_} )
    elseif( ANDROID )
        add_custom_target( ${test_name}_copy ALL ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/android ${CMAKE_CURRENT_BINARY_DIR}/${test_name} )
        add_custom_target( ${test_name}_copy_assets ALL ${CMAKE_COMMAND} -E copy_directory ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/assets ${CMAKE_CURRENT_BINARY_DIR}/${test_name}/assets )
        add_dependencies( ${test_name}_copy ${test_name}_copy_assets )
        file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${test_name}.update.cmake "execute_process(COMMAND ${SHELL_COMMAND} ${ANDROID_SDK_ROOT}/tools/android update project --target 3 --path ${CMAKE_CURRENT_BINARY_DIR}/${test_name})\n" )
        file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/${test_name}.update.cmake "execute_process(COMMAND ${SHELL_COMMAND} ant -f ${CMAKE_CURRENT_BINARY_DIR}/${test_name}/build.xml ${ANT_MODE})" )
        add_custom_command( TARGET ${test_name}_copy POST_BUILD COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/${test_name}.update.cmake )
        add_library( ${test_name} SHARED ${TEST_SRC_} androidmain.cpp )
        set_target_properties( ${test_name} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${test_name}/libs/armeabi )
        add_dependencies( ${test_name}_copy ${test_name} )
    else( WIN32 )
        add_executable( ${test_name} ${TEST_SRC_} )
    endif( WIN32 )

    #MinGW workaround (it can't find WinMain symbol in library)
    if( MINGW )
        set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -u_WinMain@16")
    endif( MINGW )

    target_link_libraries( ${test_name} TestFramework )
endmacro( ADD_TEST )

set( TEST_FRAMEWORK_SRC testframework.h testframework.cpp )

if( WIN32 )
	set( TEST_FRAMEWORK_SRC ${TEST_FRAMEWORK_SRC} winmain.cpp )
elseif( ANDROID )
	set( TEST_FRAMEWORK_SRC ${TEST_FRAMEWORK_SRC} )
else( WIN32 )
	set( TEST_FRAMEWORK_SRC ${TEST_FRAMEWORK_SRC} main.cpp )
endif( WIN32 )

add_library( TestFramework ${TEST_FRAMEWORK_SRC} )
target_link_libraries( TestFramework nane )

if( ANDROID )
    configure_file(TestFramework.ini.android ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/assets/TestFramework.ini COPYONLY) 
else( ANDROID )
    configure_file(TestFramework.ini.pc ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/assets/TestFramework.ini COPYONLY) 
endif( ANDROID )

set( TEST_SRC )

ADD_TEST( test0 )
ADD_TEST( test1 )
